package cn.mxh.pethspt.dao.impl;

import cn.mxh.pethspt.dao.DingTotalDao;
import cn.mxh.pethspt.entity.Combination;
import cn.mxh.pethspt.entity.DingTotal;
import cn.mxh.pethspt.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.util.ArrayList;
import java.util.List;

public class DingTotalDaoImpl implements DingTotalDao {
    QueryRunner queryRunner = new QueryRunner();
    /**
     * 添加订单总表
     *
     * @param dingTotal
     * @return
     */
    @Override
    public int add(DingTotal dingTotal) throws Exception {
        int num = 0;
        try{
            String sql = "insert into dingtotal(jId,uId,dingTime,dingSum) VALUES(?,?,?,?),";
            Object[] objects = new Object[]{
                    dingTotal.getjId(),
                    dingTotal.getuId(),
                    dingTotal.getDingTime(),
                    dingTotal.getDingSum()
            };
            num = queryRunner.update(DruidUtils.getConnection(),sql,objects);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 查询所有订单
     *
     * @return
     */
    @Override
    public List<DingTotal> getAll() throws Exception {
        List<DingTotal> dingTotalsList = new ArrayList<DingTotal>();
        try{
            String sql = "select * from dingtotal;";
            dingTotalsList = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<DingTotal>(DingTotal.class));
        }catch (Exception e){
            e.printStackTrace();
        }
        return dingTotalsList;
    }
}
